package com.ftwj.demo.generate.utils;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ftwj.demo.generate.entity.TableInfo;

import freemarker.template.TemplateException;

public class GeneratorUtil {
	
	private static final Logger logger=LoggerFactory.getLogger(GeneratorUtil.class);
	
	public static void generator() throws SQLException, IOException, TemplateException {
		ConnectionUtil connect=new ConnectionUtil();
		if(connect.initConnection()) {
			List<TableInfo> tables = connect.getTables();
			for (TableInfo table : tables) {
				FileUtil.generateToJava("ENTITY", table, table.getClassName()+"Bean.java");
				FileUtil.generateToJava("DAO", table, table.getClassName()+"Dao.java");
				FileUtil.generateToJava("SERVICE", table, table.getClassName()+"Service.java");
				FileUtil.generateToJava("CONTROLLER", table, table.getClassName()+"Controller.java");
				FileUtil.generateToJava("JAVASCRIPT", table, table.getClassName()+".js");
				FileUtil.generateToJava("HTML", table, table.getClassName()+"Index.html");
			}
		}
		logger.info("文件已生成：SUCCESS！");
		connect.close();
		logger.info("数据库连接：CLOSE！");
	}
	
	
	public static void generator(List<String> tableNames) throws SQLException, IOException, TemplateException {
		ConnectionUtil connect=new ConnectionUtil();
		if(connect.initConnection()) {
			List<TableInfo> tables = connect.getTables(tableNames);
			for (TableInfo table : tables) {
				FileUtil.generateToJava("ENTITY", table, table.getClassName()+"Bean.java");
				FileUtil.generateToJava("DAO", table, table.getClassName()+"Dao.java");
				FileUtil.generateToJava("SERVICE", table, table.getClassName()+"Service.java");
				FileUtil.generateToJava("CONTROLLER", table, table.getClassName()+"Controller.java");
				FileUtil.generateToJava("JAVASCRIPT", table, table.getClassName()+".js");
				FileUtil.generateToJava("HTML", table, table.getClassName()+"Index.html");
			}
		}
		logger.info("文件已生成：SUCCESS！");
		connect.close();
		logger.info("数据库连接：CLOSE！");
	}
	

}
